package personal.hmm.hpbd;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainScreen extends Activity {
	private final int STATE_MEDIA_PLAYER_PAUSE = 0;
	private final int STATE_MEDIA_PLAYER_PLAYINNG = 1;
	private final int STATE_MEDIA_PLAYER_ANIMATION = 2;
	private int stateMediaPlayer = 0;
	private int stateAnimation = 0;
	private ImageButton btnPlay;
	private ImageButton btnReplay;
	private MediaPlayer mediaPlayer;
	private ImageView imgView;
	private AnimationDrawable animation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Initialize animation
		initAnimation();
		// Button
		btnPlay = (ImageButton) findViewById(R.id.ibPlay);
		btnReplay = (ImageButton) findViewById(R.id.ibReplay);
		// Initialize Media player
		initMediaPlayer();
		playAudio();

		btnPlay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (stateMediaPlayer) {
				case STATE_MEDIA_PLAYER_PLAYINNG:
					pauseAudio();
					break;

				case STATE_MEDIA_PLAYER_PAUSE:
					playAudio();
					break;
				default:
					break;
				}
			}
		});
		btnReplay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				resetAudio();
				initMediaPlayer();
				try {
					mediaPlayer.prepare();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				playAudio();
			}
		});
	}

	private void startAnimation() {
		animation.start();
	}

	private void playAudio() {
		btnPlay.setImageResource(R.drawable.pause);
		if (stateAnimation == STATE_MEDIA_PLAYER_ANIMATION) {
			startAnimation();
		}
		stateMediaPlayer = STATE_MEDIA_PLAYER_PLAYINNG;
		mediaPlayer.start();
	}

	private void pauseAudio() {
		btnPlay.setImageResource(R.drawable.play);
		stateMediaPlayer = STATE_MEDIA_PLAYER_PAUSE;
		stateAnimation = STATE_MEDIA_PLAYER_ANIMATION;
		mediaPlayer.pause();
		stoptAnimation();
	}

	private void stoptAnimation() {
		animation.stop();
	}

	private void resetAudio() {
		mediaPlayer.stop();
		mediaPlayer.reset();
		stoptAnimation();
	}

	private void initMediaPlayer() {
		mediaPlayer = new MediaPlayer();
		mediaPlayer = MediaPlayer.create(MainScreen.this, R.raw.hmm);
		stateMediaPlayer = STATE_MEDIA_PLAYER_PAUSE;
	}

	@Override
	protected void onStop() {
		super.onStop();
		mediaPlayer.stop();
		mediaPlayer.release();
		finish();
	}

	private void initAnimation() {
		imgView = (ImageView) findViewById(R.id.ivLove);
		imgView.setBackgroundResource(R.drawable.animation);
		animation = (AnimationDrawable) imgView.getBackground();
	};

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		animation.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuAbout:
			// About
			Intent about = new Intent("personal.hmm.hpbd.ABOUT");
			startActivity(about);
			break;
		}
		return false;
	}
}
